<?php

/**
 * categories controller for categories management
 * 
 * @author HungPV <phamvanhung0818@gmail.com>
 * @package admin
 * @since 1.0
 */
class categories extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('category/category');
        $this->load->model('building/building');
    }

    function add() {
        if($this->session->userdata('login')!=1)
        {
            redirect('admin/login');
        }
        $flag = TRUE;
        $hide = TRUE;

        if ($this->input->post('name')) {
            $hide = FALSE;

            $name = $this->input->post('name');
            $description = $this->input->post('description');

            if ($this->category->add($name, $description)) {
                $flag = TRUE;
            } else {
                $flag = FALSE;
            }
        }

        $data['flag'] = $flag;
        $data['hide'] = $hide;
        $data['view'] = 'categories_add';
        $this->load->view('back_end/template', $data);
    }

    function edit($id) {
        if($this->session->userdata('login')!=1)
        {
            redirect('admin/login');
        }

        if ($this->input->post('name')) {

            $id = $this->input->post('id');
            $name = $this->input->post('name');
            $description = $this->input->post('description');
            /*$relateds = $this->input->post('ddl_related');
            $str_relate = '';
            foreach ($relateds as $value) {
                $str_relate.=$value.'@';
            }
            if(strlen($str_relate)>0)
            {
                $str_relate = substr($str_relate, 0,  strlen($str_relate)-1);
            }*/
            $str_relate = '';
            if($this->input->post('ddl_related1'))
            {
                $str_relate.= $this->input->post('ddl_related1').'@';
            }
            if($this->input->post('ddl_related2'))
            {
                $str_relate.= $this->input->post('ddl_related2').'@';
            }
            if($this->input->post('ddl_related3'))
            {
                $str_relate.= $this->input->post('ddl_related3').'@';
            }
            if(strlen($str_relate)>0)
            {
                $str_relate = substr($str_relate, 0,  strlen($str_relate)-1);
            }
            
            $this->category->edit($id,$name,$description,$str_relate);
            
            
            redirect('admin/categories/show');
        }
        $buildings = $this->building->get_buildings_by_cat($id,-1,0);
       
        $category = $this->category->get($id);
        
        $data['buildings'] = $buildings;
        $data['category'] = $category;        
        $data['view'] = 'categories_edit';
        $this->load->view('back_end/template', $data);
    }

    function show($row = 0) {
        if($this->session->userdata('login')!=1)
        {
            redirect('admin/login');
        }

        //paging
        include('paging.php');
        $config['per_page'] = 10;
        $config['base_url'] = base_url() . 'admin/categories/show/';

        $config['total_rows'] = $this->category->get_count();
        $config['cur_page'] = $row;
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();

        $categories = $this->category->get_categories($row, $config['per_page']);

        $data['categories'] = $categories;
        $data['view'] = 'categories_show';
        $this->load->view('back_end/template', $data);
    }

    function delete($id) {
        $this->category->delete($id);
    }

}

?>
